package com.sky.utils;

import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.OSSException;
import com.qiniu.common.QiniuException;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.Region;
import com.qiniu.storage.UploadManager;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import com.qiniu.util.Auth;
import com.qiniu.http.Response;
import com.qiniu.storage.model.DefaultPutRet;

import java.io.ByteArrayInputStream;

@Data
@AllArgsConstructor
@Slf4j
public class AliOssUtil {

    private String endpoint;
    private String accessKeyId;
    private String accessKeySecret;
    private String bucketName;

    /**
     * 文件上传
     *
     * @param bytes
     * @param objectName
     * @return
     */
    //public String upload(byte[] bytes, String objectName) {
    //
    //    // 创建OSSClient实例。
    //    OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
    //
    //    try {
    //        // 创建PutObject请求。
    //        ossClient.putObject(bucketName, objectName, new ByteArrayInputStream(bytes));
    //    } catch (OSSException oe) {
    //        System.out.println("Caught an OSSException, which means your request made it to OSS, "
    //                + "but was rejected with an error response for some reason.");
    //        System.out.println("Error Message:" + oe.getErrorMessage());
    //        System.out.println("Error Code:" + oe.getErrorCode());
    //        System.out.println("Request ID:" + oe.getRequestId());
    //        System.out.println("Host ID:" + oe.getHostId());
    //    } catch (ClientException ce) {
    //        System.out.println("Caught an ClientException, which means the client encountered "
    //                + "a serious internal problem while trying to communicate with OSS, "
    //                + "such as not being able to access the network.");
    //        System.out.println("Error Message:" + ce.getMessage());
    //    } finally {
    //        if (ossClient != null) {
    //            ossClient.shutdown();
    //        }
    //    }
    //
    //    //文件访问路径规则 https://BucketName.Endpoint/ObjectName
    //    StringBuilder stringBuilder = new StringBuilder("https://");
    //    stringBuilder
    //            .append(bucketName)
    //            .append(".")
    //            .append(endpoint)
    //            .append("/")
    //            .append(objectName);
    //
    //    log.info("文件上传到:{}", stringBuilder.toString());
    //
    //    return stringBuilder.toString();
    //}


    /**
     * 七牛云
     * @param bytes
     * @param objectName
     * @return
     */
    public String upload(byte[] bytes, String objectName) {

        Configuration cfg=new Configuration(Region.huabei());
        cfg.resumableUploadAPIVersion=Configuration.ResumableUploadAPIVersion.V2;
        UploadManager uploadManager=new UploadManager(cfg);
        //...生成上传凭证，然后准备上传
        String accessKey = accessKeyId;
        String secretKey = accessKeySecret;
        String bucket = bucketName;
        //默认不指定key的情况下，以文件内容的hash值作为文件名
        String key = objectName;
        try {
            Auth auth = Auth.create(accessKey, secretKey);
            String upToken = auth.uploadToken(bucket);
            try {
                Response response = uploadManager.put(bytes, key, upToken);
                //解析上传成功的结果
                //DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
                //System.out.println(putRet.key);
                //System.out.println(putRet.hash);
                return "http://"+endpoint+"/"+objectName;
            } catch (QiniuException ex) {
                Response r = ex.response;
                System.err.println(r.toString());
                try {
                    System.err.println(r.bodyString());
                } catch (QiniuException ex2) {
                    //ignore
                }
            }
        } catch (Exception ex) {
            //ignore
        }
        return null;
    }
}
